package org.adit.spring.hibernate.parser;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

import org.adit.spring.hibernate.parser.entity.Capacities;
import org.adit.spring.hibernate.parser.entity.AccumulatedLoads;
import org.adit.spring.hibernate.parser.entity.Demand;
import org.adit.spring.hibernate.parser.entity.Depot;
import org.adit.spring.hibernate.parser.entity.FleetEngineInput;
import org.adit.spring.hibernate.parser.entity.FleetEngineOutput;
import org.adit.spring.hibernate.parser.entity.Job;
import org.adit.spring.hibernate.parser.entity.Load;
import org.adit.spring.hibernate.parser.entity.MaxLoads;
import org.adit.spring.hibernate.parser.entity.Pickup;
import org.adit.spring.hibernate.parser.entity.Resource;
import org.adit.spring.hibernate.parser.entity.ResourcePeriod;
import org.adit.spring.hibernate.parser.entity.ResourceType;
import org.adit.spring.hibernate.parser.entity.Result;
import org.adit.spring.hibernate.parser.entity.StartDepot;
import org.adit.spring.hibernate.parser.entity.StopDepot;
import org.dom4j.io.XMLWriter;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class Parser {

	/**
	 * @param args
	 * @throws FileNotFoundException
	 */
	public FleetEngineInput getXmlInput(String xmlIdentifier)
			throws FileNotFoundException {

		XStream xstream = new XStream(new DomDriver());
		xstream.alias("fleetengineinput", FleetEngineInput.class);
		xstream.alias("result", Result.class);
		xstream.alias("resourceperiod", ResourcePeriod.class);
		xstream.alias("startdepot", StartDepot.class);
		xstream.alias("stopdepot", StopDepot.class);
		xstream.alias("maxloads", MaxLoads.class);
		xstream.alias("acumulatedloads", AccumulatedLoads.class);
		xstream.alias("job", Job.class);
		xstream.alias("pickup", Pickup.class);
		xstream.alias("loads", Load.class);
		xstream.alias("depot", Depot.class);
		xstream.alias("resourcetype", ResourceType.class);
		xstream.alias("resource", Resource.class);
		xstream.alias("capacities", Capacities.class);
		xstream.alias("demand", Demand.class);
		xstream.useAttributeFor(Resource.class, "id");
		xstream.useAttributeFor(Job.class, "id");

		StringBuilder text = new StringBuilder();
		String NL = System.getProperty("line.separator");
		Scanner scanner = new Scanner(new FileInputStream("c:/xml1.xml"));
		try {
			while (scanner.hasNextLine()) {
				String str = scanner.nextLine().toLowerCase();
				text.append(str + NL);
			}
		} finally {
			scanner.close();
		}

		FleetEngineInput fleetEngineInput = (FleetEngineInput) xstream
				.fromXML(text.toString());
		for (Job string : fleetEngineInput.getJobs()) {
			if (string.getId().equalsIgnoreCase(xmlIdentifier)) {

			}
		}
		return fleetEngineInput;
	}

	/**
	 * @param args
	 * @throws FileNotFoundException
	 */
	public String getXmlInputJob(String xmlIdentifier)
			throws FileNotFoundException {

		XStream xstream = new XStream(new DomDriver());
		xstream.alias("fleetengineinput", FleetEngineInput.class);
		xstream.alias("result", Result.class);
		xstream.alias("resourceperiod", ResourcePeriod.class);
		xstream.alias("startdepot", StartDepot.class);
		xstream.alias("stopdepot", StopDepot.class);
		xstream.alias("maxloads", MaxLoads.class);
		xstream.alias("acumulatedloads", AccumulatedLoads.class);
		xstream.alias("job", Job.class);
		xstream.alias("pickup", Pickup.class);
		xstream.alias("loads", Load.class);
		xstream.alias("depot", Depot.class);
		xstream.alias("resourcetype", ResourceType.class);
		xstream.alias("resource", Resource.class);
		xstream.alias("capacities", Capacities.class);
		xstream.alias("demand", Demand.class);
		xstream.useAttributeFor(Resource.class, "id");
		xstream.useAttributeFor(Job.class, "id");

		StringBuilder text = new StringBuilder();
		String NL = System.getProperty("line.separator");
		Scanner scanner = new Scanner(new FileInputStream("c:/xml1.xml"));
		try {
			while (scanner.hasNextLine()) {
				String str = scanner.nextLine().toLowerCase();
				text.append(str + NL);
			}
		} finally {
			scanner.close();
		}

		FleetEngineInput fleetEngineInput = (FleetEngineInput) xstream
				.fromXML(text.toString());
		for (Job job : fleetEngineInput.getJobs()) {
			if (job.getId().equalsIgnoreCase(xmlIdentifier)) {

				return "{name: \"store1\", location: new google.maps.LatLng("
						+ job.getPickup().getY() + "," + job.getPickup().getX()
						+ ")},";

			}
		}
		return null;
	}

	/**
	 * @param args
	 * @throws FileNotFoundException
	 */
	public FleetEngineOutput getXmlOutput(String xmlIdentifier)
			throws FileNotFoundException {

		XStream xstream = new XStream(new DomDriver());
		xstream.alias("fleetengineoutput", FleetEngineOutput.class);
		xstream.alias("result", Result.class);
		xstream.alias("resourceperiod", ResourcePeriod.class);
		xstream.alias("startdepot", StartDepot.class);
		xstream.alias("stopdepot", StopDepot.class);
		xstream.alias("maxloads", MaxLoads.class);
		xstream.alias("acumulatedloads", AccumulatedLoads.class);
		xstream.alias("job", Job.class);
		xstream.alias("pickup", Pickup.class);
		xstream.alias("load", Load.class);
		xstream.useAttributeFor(Job.class, "id");
		xstream.useAttributeFor(Resource.class, "id");
		xstream.useAttributeFor(ResourcePeriod.class, "id");

		xstream.registerConverter(new LoadConverter());
		xstream.useAttributeFor(Resource.class, "id");
		xstream.useAttributeFor(Job.class, "id");
		xstream.useAttributeFor(Load.class, "ind");
		StringBuilder text = new StringBuilder();
		String NL = System.getProperty("line.separator");
		Scanner scanner;

		if (xmlIdentifier.equals("")) {
			try {
				scanner = new Scanner(new FileInputStream("c:/xml2.xml"));

			} catch (FileNotFoundException e) {
				scanner = new Scanner(new FileInputStream(
						"/Users/edson/xml1.xml"));
			}
		} else {
			scanner = new Scanner(xmlIdentifier);
		}

		try {
			while (scanner.hasNextLine()) {
				String str = scanner.nextLine().toLowerCase();
				text.append(str + NL);
			}
		} finally {
			scanner.close();
		}

		System.out.println(text.toString());
		System.out.println(xmlIdentifier);
		System.out.println("teste");
		Parser parser = new Parser();

		FleetEngineOutput fleetEngineOutput = (FleetEngineOutput) xstream
				.fromXML(text.toString());
		for (ResourcePeriod rPeriod : fleetEngineOutput.getResourceperiods()) {
			if (rPeriod.getJobs() != null) {
				System.out.println("____" + rPeriod.getId() + "____");
				System.out.println("________");

				String jobInput = "";
				String wayPoint = "";
				for (Job job : rPeriod.getJobs()) {
					jobInput = jobInput + parser.getXmlInputJob(job.getId());
					wayPoint = wayPoint + "<p class=\"grey\">OS: "+job.getId()+"</p>";
					System.out.println(wayPoint);

					System.out.println(jobInput);
				}
			}
		}
		return fleetEngineOutput;
	}

	/**
	 * @param args
	 * @throws FileNotFoundException
	 */
	public Resource getXmlResource(String xmlIdentifier)
			throws FileNotFoundException {
		Resource resource = new Resource();
		FleetEngineInput fleetEngineOutput = getXmlInput("");

		// resource.setResourceType(fleetEngineOutput.get)
		return null;
	}

}
